﻿using System;
using System.Reflection;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YJKSparseBreadth.Properties;
using YJKGeometryFuncs;

namespace YJKSparseBreadth.ModelLayer
{
	public class AreaCheckResultBean
	{
		public Level UseLevel
		{
			get
			{
				return this.m_UseLevel;
			}
			private set
			{
				this.m_UseLevel = value;
			}
		}

		public Area UseArea
		{
			get
			{
				return this.m_UseArea;
			}
			private set
			{
				this.m_UseArea = value;
			}
		}

		public double DesignValue
		{
			get
			{
				return this.m_dDesignValue;
			}
			private set
			{
				this.m_dDesignValue = value;
			}
		}

		public double RuleValue
		{
			get
			{
				return this.m_dRuleValue;
			}
			private set
			{
				this.m_dRuleValue = value;
			}
		}

		private bool IfTruested
		{
			get
			{
				return this.m_bIfTruested;
			}
			set
			{
				this.m_bIfTruested = value;
			}
		}

		public bool IfDesignWrong
		{
			get
			{
				return this.m_bIfDesignWrong;
			}
			private set
			{
				this.m_bIfDesignWrong = value;
			}
		}

		internal AreaCheckResultBean(Level inputUseLevel, Area inputUseArea, double inputDesignValue, double inputRuleValue)
		{
			this.UseLevel = inputUseLevel;
			this.UseArea = inputUseArea;
			this.DesignValue = inputDesignValue;
			this.RuleValue = inputRuleValue;
			this.IfDesignWrong = this.Check();
		}

		public bool TryAddValue()
		{
			Schema schema = Utility.GetSchema(this.UseArea.Document, Resources.Str_GUID);
			Type typeFromHandle = typeof(InsertData);
			string text = this.ToString();
			Entity entity = new Entity(schema);
			bool result;
			try
			{
				foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
				{
					Field field = schema.GetField(fieldInfo.Name);
					if (fieldInfo.FieldType.Name.Equals(typeof(string).Name))
					{
						entity.Set<string>(field, text);
					}
				}
				this.UseArea.SetEntity(entity);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return this.UseArea.Id.IntegerValue;
		}

		public override string ToString()
		{
			string empty = string.Empty;
			return string.Concat(new string[]
			{
				this.m_UseArea.Id.IntegerValue.ToString(),
				";",
				string.Format("{0:F}", this.RuleValue),
				";",
				string.Format("{0:F}", this.DesignValue)
			});
		}

		private bool TryReadValue(out string addedValue)
		{
			Schema schema = Utility.GetSchema(this.UseArea.Document, Resources.Str_GUID);
			Type typeFromHandle = typeof(InsertData);
			addedValue = null;
			if (schema != null && schema.SchemaName.Equals(Resources.Str_SchemaName))
			{
				Entity entity = this.UseArea.GetEntity(schema);
				if (entity != null && entity.Schema != null)
				{
					foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
					{
						Field field = schema.GetField(fieldInfo.Name);
						if (fieldInfo.FieldType == typeof(string))
						{
							addedValue = entity.Get<string>(field);
							return true;
						}
					}
				}
			}
			return false;
		}

		private bool CheckTrust(string input)
		{
			double eps = 0.03;
			string[] array = input.Split(new char[]
			{
				';'
			});
			if (array.Length < 3)
			{
				return false;
			}
			int num = 0;
			double val = 0.0;
			double val2 = 0.0;
			return int.TryParse(array[0], out num) && double.TryParse(array[1], out val) && double.TryParse(array[2], out val2) && (num == this.UseArea.Id.IntegerValue && Geometry.IsEqual(this.RuleValue, val, eps) && Geometry.IsEqual(this.DesignValue, val2, eps));
		}

		private bool Check()
		{
			string empty = string.Empty;
			if (this.TryReadValue(out empty))
			{
				this.IfTruested = this.CheckTrust(empty);
			}
			return !this.IfTruested && (Geometry.LessThan(this.RuleValue, Utility.ValueZero) || Geometry.LessThan(this.DesignValue, this.RuleValue, 0.01));
		}

		private Level m_UseLevel;

		private Area m_UseArea;

		private double m_dDesignValue;

		private double m_dRuleValue;

		private bool m_bIfTruested;

		private bool m_bIfDesignWrong;
	}
}
